#!/bin/bash

SOURCE_DIR="./"
OUTPUT_DIR="./output"
R_HOME="/usr/share/R"

# 打印R的安装目录
echo "R_HOME is set to: $R_HOME"
# 检查R的头文件目录是否存在
echo "Checking R include directory: $R_HOME/include"
if [ -d "$R_HOME/include" ]; then
    echo "R include directory exists."
else
    echo "R include directory does not exist or is not a directory."
fi
# 暂停，以便检查输出
read -p "Press Enter to continue..."
# 创建输出目录
mkdir -p "$OUTPUT_DIR"
# 遍历源目录中的所有.c文件
for FILE in "$SOURCE_DIR"/*.c; do
    # 提取文件名（不带扩展名）
    BASENAME=$(basename "$FILE" .c)
    # 编译C文件
    gcc -shared -o "$OUTPUT_DIR/$BASENAME.so" -I"$R_HOME/include" -L"$R_HOME/lib" -lR "$FILE"
    # 检查编译是否成功
    if [ $? -eq 0 ]; then
        echo "Compilation of $FILE succeeded."
    else
        echo "Compilation of $FILE failed."
    fi
done
